/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.KhoanPhiDTO;

/**
 *
 * @author tuan
 */
public class KhoanPhiDAO {
    private static final String MaKhoanPhi = "MaKP0000";
    private static final int lengthMa = 8;
    
    public KhoanPhiDAO() {
    }
    
    public String getNewMaKhoanPhi() throws SQLException {
        String MaKP = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllKHOANPHIFromDataBase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MAKHOANPHI").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaKP = MaKP.concat(MaKhoanPhi + maxMa);
        return MaKP;
    }
    
    public KhoanPhiDTO getKhoanPhiByMaKP(String MaKP) throws SQLException {
        Object[] objs = { MaKP };
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetKHOANPHIFromDataBase(?)", objs);
        KhoanPhiDTO khoanPhi = new KhoanPhiDTO();
        while (rs.next()){
            khoanPhi.setMaKhoanPhi(rs.getString("MAKHOANPHI"));
            khoanPhi.setTenKhoanPhi(rs.getString("TENKHOANPHI"));
        }
        
        return khoanPhi;
    }
    public int updateHangGC(KhoanPhiDTO khoanPhi) {
        Object[] objs = { khoanPhi.getMaKhoanPhi(), khoanPhi.getTenKhoanPhi() };
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateKHOANPHIFromDataBase(?, ?)", objs);
    }
    
    public int deleteHangGCByMa(String MaKP) {
        Object[] objs = { MaKP };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteKHOANPHIFromDataBase(?)", objs);
    }
    
    public int insertHangGC(KhoanPhiDTO khoanPhi) {
        Object[] objs = { khoanPhi.getMaKhoanPhi(), khoanPhi.getTenKhoanPhi() };
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertKHOANPHIToDataBase(?, ?)", objs);
    }
}
